﻿using System;
using System.Collections.Generic;
using System.Web;


namespace CCPSIP.Reg
{
    /// <summary>CCPSIP_004：朋友確認狀態檢查 API</summary>
    /// idHeader = 裝置 UUID 前八碼。iOS 請使用 [CCPCrypto getIdHeader] 呼叫。
    /// checkcode = 裝置限時驗證碼。iOS 請使用 [CCPCrypto getCheckcode] 呼叫。

    public class confirm_status : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            resp.ContentType = "text/plain";
            resp.ContentEncoding = System.Text.Encoding.UTF8;
            DateTime reqTime = DateTime.Now;
            string resultStr = "";

            //【01】取得POST參數
            //
            string idHeader = req.Form["idHeader"];
            string checkcode = req.Form["checkcode"];

            try
            {
                Dictionary<string, string> statusDict = new Dictionary<string, string>();

                //【02】檢查裝置權限
                //
                string DeviceUUID = OracleKitReg.getAuthUUID(idHeader, checkcode);
                if (string.IsNullOrEmpty(DeviceUUID))
                {
                    throw new Exception("Error_01: Cannot identify this device");//無法識別此裝置
                }

                //【03】取得裝置的註冊狀態
                //
                string ApplyStatus = OracleKitReg.getApplyStatus(DeviceUUID);
                if (string.IsNullOrEmpty(ApplyStatus))
                {
                    throw new Exception("Error_02: Cannot find status for this device");//無法取得註冊狀態
                }

                string FriendName = OracleKitReg.getFriendName(DeviceUUID);

                if (ApplyStatus == "1")
                {
                    statusDict["StatusCode"] = "WAIT";//等待朋友確認
                    statusDict["Message"] = string.Format("Waiting for {0}'s confirmation.", FriendName)
                      + "\nYou can use other apps for now.";
                }
                else if (ApplyStatus == "Y")
                {
                    statusDict["StatusCode"] = "YES";//已通過朋友確認
                    statusDict["Message"] = string.Format("{0} confirmed your registration.", FriendName);
                }
                else if (ApplyStatus == "N")
                {
                    statusDict["StatusCode"] = "NO";//朋友退回申請
                    statusDict["Message"] = string.Format("{0} rejected your registration.", FriendName);
                }
                else
                {
                    throw new Exception("Error_03: Unknown status");//未定義的狀態碼
                }


                //【99】回傳狀態資訊
                //
                resp.ContentType = "application/json";
                resp.Charset = null;
                resultStr = ChrisKit.DictToJSON(statusDict);
            }
            catch (Exception ex)
            {
                resp.ContentType = "application/xml";
                resultStr = ChrisKit.Ex2Xml(ex, false);
            }
            finally
            {
                int contentLength = resp.ContentEncoding.GetByteCount(resultStr);
                resp.AppendHeader("Content-Length", contentLength.ToString());
                resp.Write(resultStr);
                resp.Flush();
                resp.Close();
            }
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}
